import {
  Component,
  EventEmitter,
  Input,
  OnInit,
  Output,
  SimpleChanges,
} from "@angular/core";

@Component({
  selector: "app-list",
  templateUrl: "./list.component.html",
  styleUrls: ["./list.component.less"],
})
export class ListComponent implements OnInit {
  @Input() listParams: any = {
    listData: [],
    listTotal: 0,
  };
  @Input() formParams: any = {
    pageNum: 1,
    pageSize: 10,
  };

  @Output() getList = new EventEmitter<any>();
  @Output() getSecondList = new EventEmitter<any>();

  private pageChangeTimer: any = null;
  private isSizeChange: boolean = false;

  listFirstHeader: any[] = [
    {
      title: "指令ID",
      key: "instructionId",
    },
    {
      title: "指令类型",
      key: "instructionType",
    },
    {
      title: "执行状态",
      key: "state",
    },
    {
      title: "执行结果",
      key: "result",
    },
    {
      title: "上报数据统计",
      key: "instructionStatistics",
    },
    {
      title: "IDC/ISP经营者ID",
      key: "idcId",
    },
    {
      title: "机房ID",
      key: "houseId",
    },
    {
      title: "查询起始时间",
      key: "startTime",
    },
    {
      title: "查询结束时间",
      key: "endTime",
    },
    {
      title: "指令内容",
      key: "commandInfo",
    },
    {
      title: "创建时间",
      key: "creationTime",
    },
    {
      title: "更新时间",
      key: "updatedTime",
    },
  ];
  listSecondHeader: any[] = [
    {
      title: "任务ID",
      key: "taskId",
    },
    {
      title: "执行状态",
      key: "state",
    },
    {
      title: "执行结果",
      key: "result",
    },
    {
      title: "上报数据统计",
      key: "taskStatistics",
    },
    {
      title: "任务参数",
      key: "taskArg",
    },
    {
      title: "创建时间",
      key: "createdTime",
    },
    {
      title: "更新时间",
      key: "updatedTime",
    },
  ];
  executionStatusMap: any = {
    1: "已完成",
    2: "执行中",
    3: "待执行",
  };
  resultMap: any = {
    1: "运行中",
    2: "异常",
    3: "成功",
    4: "失败",
  };
  resultColorMap: any = {
    1: "#0093dd",
    3: "#68b92e",
    2: "#ffa500",
    4: "#ff0000",
  };

  constructor() {}

  ngOnInit(): void {}

  ngOnChanges(changes: SimpleChanges): void {
    if (changes.formParams) {
      this.formParams = changes.formParams.currentValue;
    }
  }

  expandDetail(item: any) {
    if (item.expand) {
      this.getSecondList.emit(item);
    }
  }

  pageChange(number: number, type: string) {
    // 清除之前的定时器
    if (this.pageChangeTimer) {
      clearTimeout(this.pageChangeTimer);
    }

    if (type === "index") {
      // 只有在不是size变化的情况下才更新pageNum
      if (!this.isSizeChange) {
        this.formParams.pageNum = number;
      }
    } else {
      // size变化时，标记并强制重置为第一页
      this.isSizeChange = true;
      this.formParams.pageSize = number;
      this.formParams.pageNum = 1;
    }

    // 使用防抖机制，延迟100ms发送请求
    this.pageChangeTimer = setTimeout(() => {
      // 确保在size变化时pageNum始终为1
      if (this.isSizeChange) {
        this.formParams.pageNum = 1;
      }
      this.getList.emit({
        pageNum: this.formParams.pageNum,
        pageSize: this.formParams.pageSize,
      });
      this.pageChangeTimer = null;
      this.isSizeChange = false; // 重置标志
    }, 100);
  }
}
